$(function (){
    let button=$("input[type='button']");

    //登录
    button.click(function (){
        let username=$("input[name='username']").val();
        let password=$("input[name='password']").val();
        $.post({
            url: "/login", //请求路径，和form中action属性作用一样
            data: {
                "username":username,
                "password":password
            },
            dataType:"text",// Java 端返回的数据类型
            success: function (data) {
                if (data==="success") {
                    let url = location.href;
                    //获取主机地址
                    let curWwwPath=window.document.location.href;
                    let pathName=window.document.location.pathname;
                    let pos=curWwwPath.indexOf(pathName);
                    let localhostPaht=curWwwPath.substring(0,pos);
                    location.replace(localhostPaht+"/mainView") ;

                }
                else {
                    $("p").text(data);
                }
            }
        });

    })

    //添加快捷键
    $("form").keydown(function(e) {
        if (e.keyCode == '13') {//keyCode=13是回车键
            button.click();//换成按钮的id即可
        }
    });

})